package com.zat.mes.andrmes.dofun.work.vm

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.love.zywanandroid.retrofit.RetrofitManager
import com.love.zywanandroid.retrofit.api.ApiService
import com.zat.mes.andrmes.data.*
import com.zat.mes.andrmes.retrofit.callback.MesCallBack
import kotlinx.android.synthetic.main.activity_disassemble.*
import okhttp3.RequestBody
import org.json.JSONObject
import retrofit2.Call
import retrofit2.Response

/**
 * @author
 * @date :
 * @description:
 */
class MotorWorkRecordModel : ViewModel() {

     private val motorWorkRecordData: MutableLiveData<MutableList<MotorWorkRecordBean>> = MutableLiveData()
    private val updateData: MutableLiveData<String> = MutableLiveData()

    fun getMotorWorkRecordData(): MutableLiveData<MutableList<MotorWorkRecordBean>> {
        return motorWorkRecordData
    }
    fun getUpdateData(): MutableLiveData<String >{
        return updateData
    }
    /**
     * app-查询机台的生产计划
     * GET /program/scheduling/findProgramByMachineIddDate
     */
    fun findProgramByMachineIddDate(  day: String,qrCode:String) {
        if (qrCode.isEmpty()){
            return;
        }

        Log.d("--------",qrCode)
        var day1=day.replace("-","/")
        var machineIds=qrCode.split("@")
        RetrofitManager.apiService(ApiService::class.java)
           // .findProgramByMachineIddDate( day1,  "625")
            .findProgramByMachineIddDate( day1,if(machineIds.size>2) machineIds[1]else "")
            .enqueue(object : MesCallBack<BaseResponse<ArrayList<MotorWorkRecordBean>>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<ArrayList<MotorWorkRecordBean>>>?,
                    response: Response<BaseResponse<ArrayList<MotorWorkRecordBean>>>?
                ) {
                    val result = response?.body()?.result
                    motorWorkRecordData.postValue( result as MutableList<MotorWorkRecordBean>?)
                }

            })

    }

    fun updateProducedQty(  produceQty: String,programId:String) {

        val json = JSONObject()
        json.put("produceQty",  produceQty)
        json.put("programId", programId)
        val requestBody: RequestBody = RequestBody.create(RetrofitManager.JOSNTYPE, json.toString())


        RetrofitManager.apiService(ApiService::class.java)
            .updateProducedQty( requestBody)
            .enqueue(object :MesCallBack<BaseResponse<String>>(){
                override fun onSuccessful(
                    call: Call<BaseResponse<String>>?,
                    response: Response<BaseResponse<String>>?
                ) {
                    updateData.postValue(response?.body()?.message)
                }

            })

    }




}